﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using SD.LLBLGen.Pro.ORMSupportClasses;
using DatabaseAdapter;
using DatabaseGeneric;
using DatabaseGeneric.FactoryClasses;
using DatabaseGeneric.EntityClasses;
using DatabaseGeneric.RelationClasses;
using DatabaseGeneric.HelperClasses;

namespace TKCCLD
{
    public partial class FormSanPhamBo : DevExpress.XtraEditors.XtraForm
    {
        public FormSanPhamBo()
        {
            InitializeComponent();
        }

        private FormMain formMain;
        public FormSanPhamBo(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;

            currentCollectionChungLoaiSanPham = iChungLoaiSanPham.FetchCollection();
            sourceChungLoaiSanPham.DataSource = currentCollectionChungLoaiSanPham;
            repository.FetchState("IsFetch", barManager);

            if (String.Equals(TKCCLD.Right.Role, "Users"))
            {
                SetRight();
            }
        }

        private Interfaces.IChungLoaiSanPham iChungLoaiSanPham = new Services.ChungLoaiSanPham();
        private EntityCollection<ChungLoaiSanPhamEntity> currentCollectionChungLoaiSanPham = null;
        private ChungLoaiSanPhamEntity currentEntityChungLoaiSanPham = null;

        private Interfaces.ISanPhamBo iSanPhamBo = new Services.SanPhamBo();
        private EntityCollection<SanPhamBoEntity> currentCollectionSanPhamBo = null;
        private SanPhamBoEntity currentEntitySanPhamBo = null;

        private Repository repository = new Repository();

        private void SetRight()
        {
            repository.FetchRight(barManager);
            this.KeyPreview = false;
            colMaChungLoaiBo.OptionsColumn.AllowEdit = false;
            colTenChungLoaiBo.OptionsColumn.AllowEdit = false;
            colBanCat.OptionsColumn.AllowEdit = false;
            colBanThanhPham.OptionsColumn.AllowEdit = false;
            colMaSanPhamBo.OptionsColumn.AllowEdit = false;
            colHinhSanPhamBo.OptionsColumn.AllowEdit = false;
            colSoLuong.OptionsColumn.AllowEdit = false;
            colVatTu.OptionsColumn.AllowEdit = false;            
            colGhiChu.OptionsColumn.AllowEdit = false;
        }

        private void lookupMaChungLoaiSanPham_EditValueChanged(object sender, EventArgs e)
        {
            currentEntityChungLoaiSanPham = lookupMaChungLoaiSanPham.Properties.GetDataSourceRowByKeyValue(lookupMaChungLoaiSanPham.EditValue) as ChungLoaiSanPhamEntity;
            textTenChungLoaiSanPham.DataBindings.Clear();
            textTenChungLoaiSanPham.DataBindings.Add("EditValue", currentEntityChungLoaiSanPham, "TenChungLoaiSanPham");

            currentCollectionSanPhamBo = iSanPhamBo.FetchCollection(currentEntityChungLoaiSanPham);
            sourceSanPhamBo.DataSource = currentCollectionSanPhamBo;
        }

        private void sourceSanPhamBo_CurrentChanged(object sender, EventArgs e)
        {
            currentEntitySanPhamBo = sourceSanPhamBo.Current as SanPhamBoEntity;
        }

        private void barAddnew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (currentEntityChungLoaiSanPham == null)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn chủng loại sản phẩm !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if ((currentEntitySanPhamBo == null) || ((!currentEntitySanPhamBo.IsNew) && (!currentEntitySanPhamBo.IsDirty)))
                {
                    SanPhamBoEntity entitySanPhamBo = new SanPhamBoEntity();
                    entitySanPhamBo.MaChungLoaiSanPham = currentEntityChungLoaiSanPham.MaChungLoaiSanPham;
                    currentCollectionSanPhamBo.Add(entitySanPhamBo);
                    sourceSanPhamBo.Position = sourceSanPhamBo.Count - 1;
                    repository.FetchState("IsNew", barManager);
                }
            }
        }

        private void barRemove_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (currentEntitySanPhamBo.IsNew)
            {
                currentCollectionSanPhamBo.Remove(currentEntitySanPhamBo);
                repository.FetchState("IsFetch", barManager);
            }
        }

        private void barUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            repository.CloseEditor(gridViewSanPhamBo);
            if (currentEntitySanPhamBo.IsNew || currentEntitySanPhamBo.IsDirty)
            {
                iSanPhamBo.SaveCollection(currentCollectionSanPhamBo);
                gridViewSanPhamBo.RefreshRow(gridViewSanPhamBo.FocusedRowHandle);
                repository.FetchState("IsFetch", barManager);
            }
        }

        private void barReject_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            repository.CloseEditor(gridViewSanPhamBo);
            if (currentEntitySanPhamBo.IsDirty)
            {
                SanPhamBoEntity storageEntitySanPhamBo = iSanPhamBo.FetchEntity(currentEntitySanPhamBo);
                repository.FetchField(currentEntitySanPhamBo, storageEntitySanPhamBo);
                gridViewSanPhamBo.RefreshRow(gridViewSanPhamBo.FocusedRowHandle);
                repository.FetchState("IsFetch", barManager);
            }
        }

        private void barDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (currentEntityChungLoaiSanPham == null)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn chủng loại sản phẩm !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (currentEntitySanPhamBo == null)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng chọn bọ để xóa !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    if ((!currentEntitySanPhamBo.IsNew) && (!currentEntitySanPhamBo.IsDirty))
                    {
                        DialogResult dialogResult = DevExpress.XtraEditors.XtraMessageBox.Show("Bạn muốn xóa bọ " + currentEntitySanPhamBo.MaSanPhamBo + " không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dialogResult == DialogResult.Yes)
                        {
                            iSanPhamBo.DeleteEntity(currentEntitySanPhamBo);
                            currentCollectionSanPhamBo.Remove(currentEntitySanPhamBo);
                            repository.FetchState("IsFetch", barManager);
                        }
                    }
                }
            }
        }

        private void barReport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataTable dtSanPhamBo = iSanPhamBo.Report(currentEntityChungLoaiSanPham);
            if (dtSanPhamBo.Rows.Count == 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Không có dữ liệu !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                stiReport.RegData(dtSanPhamBo);
                stiReport.Dictionary.Synchronize();
                Stimulsoft.Report.Components.StiDataBand stiDataBand = stiReport.Pages["Page"].Components["DataBand"] as Stimulsoft.Report.Components.StiDataBand;
                stiDataBand.DataSourceName = "SanPhamBoByChungLoaiSanPhamPro";
                stiReport.Dictionary.Variables["MaChungLoaiSanPham"].Value = (string)lookupMaChungLoaiSanPham.EditValue;
                stiReport.ResetRenderedState();
                stiReport.InvokeRefreshViewer();
                stiReport.Show();
            }
        }

        private void barClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void FormSanPhamBo_FormClosed(object sender, FormClosedEventArgs e)
        {
            formMain.pFormSanPhamBo = null;
        }

        private void gridViewSanPhamBo_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if ((!currentEntitySanPhamBo.IsNew) && (!currentEntitySanPhamBo.IsDirty) && (gridViewSanPhamBo.IsEditing) && (!gridViewSanPhamBo.IsFilterRow(gridViewSanPhamBo.FocusedRowHandle)))
            {
                repository.FetchState("IsDirty", barManager);
            }
        }

        private void gridViewSanPhamBo_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (!gridViewSanPhamBo.IsFilterRow(e.RowHandle)))
            {
                int rowHandle = e.RowHandle + 1;
                e.Info.DisplayText = rowHandle.ToString();
            }
        }

        private void FormSanPhamBo_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1:
                    barAddnew_ItemClick(sender, null);
                    break;
                case Keys.F2:
                    barRemove_ItemClick(sender, null);
                    break;
                case Keys.F3:
                    barUpdate_ItemClick(sender, null);
                    break;
                case Keys.F4:
                    barReject_ItemClick(sender, null);
                    break;
                case Keys.F5:
                    barDelete_ItemClick(sender, null);
                    break;
                case Keys.F6:
                    barReport_ItemClick(sender, null);
                    break;
                case Keys.F7:
                    barClose_ItemClick(sender, null);
                    break;
            }
        }

        private void FormSanPhamBo_Load(object sender, EventArgs e)
        {

        }
    }
}